﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DataAccess;

namespace iEN.PlatformSet
{
    public partial class CreateUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["SysAdmin"] != null)
                {
                    initMenu();
                }
                else
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }
            }
        }

        private void initMenu()
        {
            cbEnable.Checked = true;
            rblUsertype.SelectedValue = "0";

            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                lblMsg.Visible = true;
            }
        }

        protected void backSetUser_Click(object sender, EventArgs e)
        {
            this.Page.Response.Redirect("SetUser.aspx");
        }

        protected void bCreateUser_Click(object sender, EventArgs e)
        {
            if (tbAccount.Text == "")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('新增帳號失敗，登入帳號不得為空')", true);
                return;
            }

            if (tbPassword.Text == "")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('新增帳號失敗，登入密碼不得為空')", true);
                return;
            }

            // DataTable dt = (DataTable)Session["UserDetailData"];
            DataTable dt = new DBAccess().getUserData("1");
            if (dt != null)
                for (int i = 0; i < dt.Rows.Count; i++)
                    if (dt.Rows[i]["_account"].ToString() == tbAccount.Text)
                    {
                        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('帳號" + tbAccount.Text + "已存在，新增帳號失敗" + "')", true);
                        return;
                    }

            bool createResult;

            createResult = new DBAccess().createUserData(cbEnable.Checked.ToString(),
                                                          tbUsername.Text,
                                                          rblUsertype.SelectedValue,
                                                          tbAccount.Text,
                                                          tbPassword.Text,
                                                          tbPhone.Text,
                                                          tbMobilephone.Text,
                                                          tbEmail.Text);

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "Account：" + tbAccount.Text + "//名稱：" + tbUsername.Text;

            if (createResult == true)
            {

                new DBAccess().addEventLog(strAccount, strIP, 11, strMemo, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('新增帳號資料成功')", true);
                // lShowmsg.Text = "新增帳號資料成功";
                // lShowmsg.Visible = true;
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 11, strMemo, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('新增帳號資料失敗；請稍後再試')", true);
                // lShowmsg.Text = "新增帳號資料失敗；請稍後再試";
                // lShowmsg.Visible = true;
            }
        }


    }
}